package com.alili_tv.system.utils;

import org.springframework.util.DigestUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 账号加密前添加前缀
 */
public class CommonUtils {
    private static final String prefix = "prefix_str";

    /**
     * md5加密
     * @param pwd
     * @return
     */
    public static String md5Encryption(String pwd) {
        return DigestUtils.md5DigestAsHex((prefix + pwd).getBytes());
    }

    /**
     * 判断当前系统是否为linux
     * @return
     */
    public static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().contains("linux");
    }

    /**
     * 判断当前系统是否为windows
     * @return
     */
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }

    /**
     * Stream流的工具, 按照某个字段进行去重
     * @param keyExtractor
     * @return
     * @param <T>
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new HashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
